<?php

defined('SYSPATH') or die('No direct access allowed.');

class Controller_Books extends Controller_Site {

    public function action_index() {
        if ($this->request->method() == 'POST') {
            if ($this->request->post('search_category') == 'media') {
                $result = ORM::factory('books')->where('title', 'LIKE', '%' . $this->request->post('search') . '%')->find_all();
                $result_type = 'media';
            }

            if ($this->request->post('search_category') == 'world') {
                $world = ORM::factory('world')->where('name', 'LIKE', '%' . $this->request->post('search') . '%')->find_all();
                $key = 0;
                foreach ($world as $w) {
                    $media = ORM::factory('books')->where('world_id', '=', $w->id)->find_all();
                    foreach ($media as $m) {
                        $result[$key] = $m;
                        $key++;
                    }
                }
                $result_type = 'world';
            }

            if ($this->request->post('search_category') == 'isbn') {
                $result = ORM::factory('books')->where('ISBN', '=', $this->request->post('search'))->find_all();
                $result_type = 'isbn';
            }

            if ($this->request->post('search_category') == 'series') {
                $result = ORM::factory('books')->where('series', '=', $this->request->post('search'))->find_all();
                $result_type = 'series';
            }

            if ($this->request->post('search_category') == 'author') {
                $names = explode(' ', $this->request->post('search'));
                $key = 0;
                foreach ($names as $name) {
                    $authors = ORM::factory('author')->where('first_name', 'LIKE', $name . '%')->find_all();
                    foreach ($authors as $author) {
                        $result[$key] = $author;
                        $key++;
                    }
                }
                foreach ($names as $name) {
                    $authors = ORM::factory('author')->where('second_name', 'LIKE', $name . '%')->find_all();
                    foreach ($authors as $author) {
                        $result[$key] = $author;
                        $key++;
                    }
                }
                $result_type = 'author';
            }

            $this->content->set('result', $result);
            $this->content->set('result_type', $result_type);
        }
    }

    public function action_showmedia() {
        $id = $this->request->param('id');
        $book = ORM::factory('books')->where('id', '=', $id)->find();
        $authors = ORM::factory('authormedia')->where('media_id', '=', $id)->find_all();
        $this->content->set('book', $book);
        $this->content->set('authors', $authors);
    }

    public function action_showauthor() {
        $id = $this->request->param('id');
        $author = ORM::factory('author')->where('id', '=', $id)->find();
        $this->content->set('author', $author);
    }

    public function action_showworld() {
        $id = $this->request->param('id');
        $world = ORM::factory('world')->where('id', '=', $id)->find();
        $this->content->set('world', $world);
    }

    public function action_loan() {
        $id = $this->request->param('id');
        $loan = ORM::factory('loan')->where('media_id', '=', $id)->find_all();
        $loan_count = $loan->count();
        $media = ORM::factory('books')->where('id', '=', $id)->find();
        $media_count = $media->quantity;
        $user_id = SESSION::instance()->get('user_id');

        $queue = ORM::factory('queue')->where('user_id', '=', $user_id)->where('media_id', '=', $id)->find();
        if (!$queue->loaded()) {
            $queue = ORM::factory('queue');
            $queue->user_id = $user_id;
            $queue->media_id = $id;
            $queue->save();

            $media_queue = ORM::factory('queue')->where('media_id', '=', $id)->find_all();
            $userkey = 0;
            foreach ($media_queue as $key => $mq) {
                if(($mq->user_id==$user_id)&&($mq->media_id==$id)){
                    $userkey=$key;
                }
            }

            if ($media_count > $loan_count) {
                
            } else {
                
            }
        }
    }

}